package com.group.service;

import com.group.view.KeyBean;
import com.group.view.SpotFuturesView;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 现货期货交易对
 */
@Service
public class ExchangeService {
    /**
     * 组合交易对
     *
     * @return
     */
    public List<SpotFuturesView> getSpotFuturesViewList() {

        List<SpotFuturesView> sfList = new ArrayList<>();
        List<KeyBean> list = new ArrayList<>();
        int id = 0;

        //kbitmex
        KeyBean kbitmex = new KeyBean("bitcoin", "USD", "bitmex", "XBTUSD");

        //okex
        list.add(new KeyBean("bitcoin", "tether", "okex"));

        //huobiPro
        list.add(new KeyBean("bitcoin", "tether", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("bitcoin", "USD", "bitfinex"));

        //Binance
        list.add(new KeyBean("bitcoin", "tether", "binance"));

        //zb
        list.add(new KeyBean("bitcoin", "tether", "zb"));

        //Hitbtc
        list.add(new KeyBean("bitcoin", "tether", "hitbtc"));

        //Kraken
        list.add(new KeyBean("bitcoin", "USD", "kraken"));

        //OKExFutures
        list.add(new KeyBean("bitcoin", "USD", "this_week", "okexFutures", null));
        list.add(new KeyBean("bitcoin", "USD", "next_week", "okexFutures", null));
        list.add(new KeyBean("bitcoin", "USD", "quarter", "okexFutures", null));

        setList(id++, kbitmex, sfList, list);

        KeyBean kbitmex2 = new KeyBean("cardano", "bitcoin", "bitmex", "ADAM18");

        //okex

        //huobiPro
        list.add(new KeyBean("cardano", "bitcoin", "huobiPro"));

        //bitfinex

        //Binance
        list.add(new KeyBean("cardano", "bitcoin", "binance"));

        //zb

        //Hitbtc
        list.add(new KeyBean("cardano", "bitcoin", "hitbtc"));

        //Kraken

        //OKExFutures

        setList(id++, kbitmex2, sfList, list);

        KeyBean kbitmex3 = new KeyBean("bitcoin-cash", "bitcoin", "bitmex", "BCHM18");

        //okex
        list.add(new KeyBean("bitcoin-cash", "bitcoin", "okex"));

        //huobiPro
        list.add(new KeyBean("bitcoin-cash", "bitcoin", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("bitcoin-cash", "bitcoin", "bitfinex"));

        //Binance
        list.add(new KeyBean("bitcoin-cash", "bitcoin", "binance"));

        //zb

        //Hitbtc
        list.add(new KeyBean("bitcoin-cash", "bitcoin", "hitbtc"));

        //Kraken
        list.add(new KeyBean("bitcoin-cash", "bitcoin", "kraken"));

        //OKExFutures

        setList(id++, kbitmex3, sfList, list);

        KeyBean kbitmex4 = new KeyBean("ethereum", "bitcoin", "bitmex", "ETHM18");

        //okex
        list.add(new KeyBean("ethereum", "bitcoin", "okex"));

        //huobiPro
        list.add(new KeyBean("ethereum", "bitcoin", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("ethereum", "bitcoin", "bitfinex"));

        //Binance
        list.add(new KeyBean("ethereum", "bitcoin", "binance"));

        //zb
        list.add(new KeyBean("ethereum", "bitcoin", "zb"));

        //Hitbtc
        list.add(new KeyBean("ethereum", "bitcoin", "hitbtc"));

        //Kraken
        list.add(new KeyBean("ethereum", "bitcoin", "kraken"));

        //OKExFutures

        setList(id++, kbitmex4, sfList, list);

        KeyBean kbitmex5 = new KeyBean("litecoin", "bitcoin", "bitmex", "LTCM18");

        //okex
        list.add(new KeyBean("litecoin", "bitcoin", "okex"));

        //huobiPro
        list.add(new KeyBean("litecoin", "bitcoin", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("litecoin", "bitcoin", "bitfinex"));

        //Binance
        list.add(new KeyBean("litecoin", "bitcoin", "binance"));

        //zb
        list.add(new KeyBean("litecoin", "bitcoin", "zb"));

        //Hitbtc
        list.add(new KeyBean("litecoin", "bitcoin", "hitbtc"));

        //Kraken
        list.add(new KeyBean("litecoin", "bitcoin", "kraken"));

        //OKExFutures

        setList(id++, kbitmex5, sfList, list);

        KeyBean kbitmex6 = new KeyBean("ripple", "bitcoin", "bitmex", "XRPM18");

        //okex
        list.add(new KeyBean("ripple", "bitcoin", "okex"));

        //huobiPro
        list.add(new KeyBean("ripple", "bitcoin", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("ripple", "bitcoin", "bitfinex"));

        //Binance
        list.add(new KeyBean("ripple", "bitcoin", "binance"));

        //zb
        list.add(new KeyBean("ripple", "bitcoin", "zb"));

        //Hitbtc
        list.add(new KeyBean("ripple", "bitcoin", "hitbtc"));

        //Kraken
        list.add(new KeyBean("ripple", "bitcoin", "kraken"));

        //OKExFutures

        setList(id++, kbitmex6, sfList, list);

        //OkexFutures
        List<KeyBean> copyList1 = new ArrayList<>();
        List<KeyBean> copyList2 = new ArrayList<>();
        //本周
        KeyBean kOkexFutures = new KeyBean("bitcoin", "USD", "this_week", "okexFutures", null);

        //okex
        list.add(new KeyBean("bitcoin", "tether", "okex"));

        //huobiPro
        list.add(new KeyBean("bitcoin", "tether", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("bitcoin", "USD", "bitfinex"));

        //Binance
        list.add(new KeyBean("bitcoin", "tether", "binance"));

        //zb
        list.add(new KeyBean("bitcoin", "tether", "zb"));

        //Hitbtc
        list.add(new KeyBean("bitcoin", "tether", "hitbtc"));

        //Kraken
        list.add(new KeyBean("bitcoin", "USD", "kraken"));

        //bitmex
        list.add(new KeyBean("bitcoin", "USD", "bitmex", "XBTUSD"));

        //OKExFutures
        id = setOkexFuturesListData(list, copyList1, copyList2, id, kOkexFutures, "bitcoin", "USD", sfList);

        KeyBean kOkexFutures1 = new KeyBean("litecoin", "USD", "this_week", "okexFutures", null);

        //okex
        list.add(new KeyBean("litecoin", "tether", "okex"));

        //huobiPro
        list.add(new KeyBean("litecoin", "tether", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("litecoin", "tether", "bitfinex"));

        //Binance
        list.add(new KeyBean("litecoin", "tether", "binance"));

        //zb
        list.add(new KeyBean("litecoin", "tether", "zb"));

        //Hitbtc
        list.add(new KeyBean("litecoin", "tether", "hitbtc"));

        //Kraken
        list.add(new KeyBean("litecoin", "USD", "kraken"));

        //bitmex

        //OKExFutures
        id = setOkexFuturesListData(list, copyList1, copyList2, id, kOkexFutures1, "litecoin", "USD", sfList);

        KeyBean kOkexFutures2 = new KeyBean("ethereum", "USD", "this_week", "okexFutures", null);

        //okex
        list.add(new KeyBean("ethereum", "tether", "okex"));

        //huobiPro
        list.add(new KeyBean("ethereum", "tether", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("ethereum", "USD", "bitfinex"));

        //Binance
        list.add(new KeyBean("ethereum", "tether", "binance"));

        //zb
        list.add(new KeyBean("ethereum", "tether", "zb"));

        //Hitbtc
        list.add(new KeyBean("ethereum", "tether", "hitbtc"));

        //Kraken
        list.add(new KeyBean("ethereum", "USD", "kraken"));

        //bitmex
        list.add(new KeyBean("ethereum", "bitcoin", "bitmex", "ETHM18"));

        //OKExFutures
        id = setOkexFuturesListData(list, copyList1, copyList2, id, kOkexFutures2, "ethereum", "USD", sfList);

        KeyBean kOkexFutures3 = new KeyBean("ethereum-classic", "USD", "this_week", "okexFutures", null);

        //okex
        list.add(new KeyBean("ethereum-classic", "tether", "okex"));

        //huobiPro
        list.add(new KeyBean("ethereum-classic", "tether", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("ethereum-classic", "USD", "bitfinex"));

        //Binance

        //zb
        list.add(new KeyBean("ethereum-classic", "tether", "zb"));

        //Hitbtc
        list.add(new KeyBean("ethereum-classic", "tether", "hitbtc"));

        //Kraken
        list.add(new KeyBean("ethereum-classic", "USD", "kraken"));

        //bitmex

        //OKExFutures
        id = setOkexFuturesListData(list, copyList1, copyList2, id, kOkexFutures3, "ethereum-classic", "USD", sfList);

        KeyBean kOkexFutures4 = new KeyBean("bitcoin-cash", "USD", "this_week", "okexFutures", null);

        //okex
        list.add(new KeyBean("bitcoin-cash", "tether", "okex"));

        //huobiPro
        list.add(new KeyBean("bitcoin-cash", "tether", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("bitcoin-cash", "USD", "bitfinex"));

        //Binance
        list.add(new KeyBean("bitcoin-cash", "tether", "binance"));

        //zb

        //Hitbtc
        list.add(new KeyBean("bitcoin-cash", "tether", "hitbtc"));

        //Kraken
        list.add(new KeyBean("bitcoin-cash", "USD", "kraken"));

        //bitmex

        //OKExFutures
        id = setOkexFuturesListData(list, copyList1, copyList2, id, kOkexFutures4, "bitcoin-cash", "USD", sfList);

        KeyBean kOkexFutures5 = new KeyBean("bitcoin-gold", "USD", "this_week", "okexFutures", null);

        //okex
        list.add(new KeyBean("bitcoin-gold", "tether", "okex"));

        //huobiPro

        //bitfinex
        list.add(new KeyBean("bitcoin-gold", "USD", "bitfinex"));

        //Binance

        //zb

        //Hitbtc
        list.add(new KeyBean("bitcoin-gold", "tether", "hitbtc"));

        //Kraken

        //bitmex

        //OKExFutures
        id = setOkexFuturesListData(list, copyList1, copyList2, id, kOkexFutures5, "bitcoin-gold", "USD", sfList);

        KeyBean kOkexFutures6 = new KeyBean("ripple", "USD", "this_week", "okexFutures", null);

        //okex
        list.add(new KeyBean("ripple", "tether", "okex"));

        //huobiPro
        list.add(new KeyBean("ripple", "tether", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("ripple", "USD", "bitfinex"));

        //Binance
        list.add(new KeyBean("ripple", "tether", "binance"));

        //zb
        list.add(new KeyBean("ripple", "tether", "zb"));

        //Hitbtc
        list.add(new KeyBean("ripple", "tether", "hitbtc"));

        //Kraken
        list.add(new KeyBean("ripple", "USD", "kraken"));

        //bitmex

        //OKExFutures
        id = setOkexFuturesListData(list, copyList1, copyList2, id, kOkexFutures6, "ripple", "USD", sfList);

        KeyBean kOkexFutures7 = new KeyBean("eos", "USD", "this_week", "okexFutures", null);

        //okex
        list.add(new KeyBean("eos", "tether", "okex"));

        //huobiPro
        list.add(new KeyBean("eos", "tether", "huobiPro"));

        //bitfinex
        list.add(new KeyBean("eos", "USD", "bitfinex"));

        //Binance

        //zb
        list.add(new KeyBean("eos", "tether", "zb"));

        //Hitbtc
        list.add(new KeyBean("eos", "tether", "hitbtc"));

        //Kraken
        list.add(new KeyBean("eos", "USD", "kraken"));

        //bitmex

        //OKExFutures
        setOkexFuturesListData(list, copyList1, copyList2, id, kOkexFutures7, "eos", "USD", sfList);
        return sfList;
    }

    /**
     * 添加不同合约类型的数据
     *
     * @param list
     * @param copyList1
     * @param copyList2
     * @param id
     * @param keybean
     * @param baseAsset
     * @param quoteAsset
     * @param sfList
     * @return
     */
    private int setOkexFuturesListData(List<KeyBean> list, List<KeyBean> copyList1, List<KeyBean> copyList2, int id, KeyBean keybean, String baseAsset, String quoteAsset, List<SpotFuturesView> sfList) {

        if (list == null || keybean == null || baseAsset == null || quoteAsset == null || sfList == null) {
            return id;
        }

        copyList1.addAll(list);
        copyList2.addAll(list);

        setOkexFuturesList(id++, keybean, "this_week", baseAsset, quoteAsset, sfList, list);

        setOkexFuturesList(id++, keybean, "next_week", baseAsset, quoteAsset, sfList, copyList1);

        setOkexFuturesList(id++, keybean, "quarter", baseAsset, quoteAsset, sfList, copyList2);

        return id;
    }

    /**
     * 为每一个合约类型，添加剩余的2个合约类型
     *
     * @param id
     * @param keybean
     * @param contractType 合约类型
     * @param baseAsset
     * @param quoteAsset
     * @param sfList
     * @param list
     */
    private void setOkexFuturesList(int id, KeyBean keybean, String contractType, String baseAsset, String quoteAsset, List<SpotFuturesView> sfList, List<KeyBean> list) {

        if (baseAsset == null || quoteAsset == null || contractType == null) {
            return;
        }

        if (contractType.equals("this_week")) {

            list.add(new KeyBean(baseAsset, quoteAsset, "next_week", "okexFutures", null));
            list.add(new KeyBean(baseAsset, quoteAsset, "quarter", "okexFutures", null));
            setList(id, keybean, sfList, list);

        } else if (contractType.equals("next_week")) {

            KeyBean nextWeek = new KeyBean();
            BeanUtils.copyProperties(keybean, nextWeek);
            nextWeek.setContractType(contractType);
            list.add(new KeyBean(baseAsset, quoteAsset, "this_week", "okexFutures", null));
            list.add(new KeyBean(baseAsset, quoteAsset, "quarter", "okexFutures", null));
            setList(id, nextWeek, sfList, list);

        } else if (contractType.equals("quarter")) {

            KeyBean quarter = new KeyBean();
            BeanUtils.copyProperties(keybean, quarter);
            quarter.setContractType(contractType);
            list.add(new KeyBean(baseAsset, quoteAsset, "this_week", "okexFutures", null));
            list.add(new KeyBean(baseAsset, quoteAsset, "next_week", "okexFutures", null));
            setList(id, quarter, sfList, list);

        }
    }

    /**
     * 将每一条具体的合约类型数据添加到 sfList 中
     *
     * @param id
     * @param keyBean
     * @param sfList
     * @param list
     */
    private void setList(int id, KeyBean keyBean, List<SpotFuturesView> sfList, List<KeyBean> list) {

        List<KeyBean> cp = new ArrayList<>();
        cp.addAll(list);

        sfList.add(new SpotFuturesView(id, keyBean, cp));
        list.removeAll(list);

    }
}

